from django.db import models
from apps.system.models import Upload, Option
from utils.exceptions import CustomError


class House(models.Model):
    address = models.CharField(verbose_name='地址', max_length=250)
    style = models.ForeignKey(Option, verbose_name='装修风格', on_delete=models.PROTECT)
    describe = models.CharField(verbose_name='描述', max_length=250)
    area = models.IntegerField(verbose_name='面积')
    collect_count = models.IntegerField(verbose_name='收藏量', default=0)
    thumbnail = models.ForeignKey(Upload, verbose_name='封面图片', on_delete=models.PROTECT, related_name='cover_path',
                                  null=True, blank=True)
    video = models.ForeignKey(Upload, verbose_name='视频', on_delete=models.PROTECT, related_name='video_path',
                              null=True, blank=True)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    delete = models.BooleanField(verbose_name='删除', default=False)
    is_complete = models.BooleanField(verbose_name='是否完工', default=True)

    class Meta:
        db_table = 'house'
        verbose_name = '房子'
        ordering = ['-id']

    @staticmethod
    def get_instance_by_id(id):
        try:
            id = int(id)
        except:
            raise CustomError('无效的id')

        instance = House.objects.filter(pk=id).first()
        if not instance:
            raise CustomError('未找到房屋对应的实例')
        return instance


class Room(models.Model):
    house = models.ForeignKey(House, verbose_name='房子', related_name='house_rooms', on_delete=models.PROTECT)
    name = models.CharField(verbose_name='房间', max_length=20)
    describe = models.CharField(verbose_name='描述', max_length=200)
    thumbnail = models.ForeignKey(Upload, verbose_name='封面图片', on_delete=models.PROTECT)
    delete = models.BooleanField(verbose_name='删除', default=False)

    class Meta:
        db_table = 'house_room'
        verbose_name = '房间'
        ordering = ['-id']

    @staticmethod
    def get_instance_by_id(id):
        try:
            id = int(id)
        except:
            raise CustomError('无效的id')

        instance = Room.objects.filter(pk=id).first()
        if not instance:
            raise CustomError('未找到房间对应的实例')
        return instance
